<script setup lang="ts">
import { PatientAddAPI, PatientInfoAPI, PatientUpdateAPI } from '@/apis/my'
import type { PatientAddParams } from '@/types/my'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
const addPatientParams = ref<PatientAddParams>({
  defaultFlag: 1,
  gender: 1,
  idCard: '',
  name: '',
})
// 问诊-添加患者信息
const PatientAdd = async () => {
  const res = await PatientAddAPI(addPatientParams.value)
  // console.log(res)
}
// 默认患者
const changeSwitch = (e: UniHelper.SwitchOnChangeEvent) => {
  return e.detail.value
    ? (addPatientParams.value.defaultFlag = 1)
    : (addPatientParams.value.defaultFlag = 0)
}
const rules = ref<UniHelper.UniFormsRules>({
  name: {
    rules: [{ required: true, errorMessage: '请填写姓名' }],
  },
  idCard: {
    rules: [
      { required: true, errorMessage: '请填写身份证号' },
      {
        pattern:
          /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
        errorMessage: '身份证号格式不正确',
      },
    ],
  },
  gender: {
    rules: [
      { required: true, errorMessage: '请选择性别' },
      {
        validateFunction(rule, value, data, callback) {
          const idCardGenderNum = addPatientParams.value.idCard.charAt(16)
          if (Number(idCardGenderNum) % 2 !== value) {
            callback('性别选择与身份证号不符')
          }
          return true
        },
      },
    ],
  },
})
const formRef = ref<UniHelper.UniForms>()
const props = defineProps<{
  id: string
}>()
// 获取患者详情
const PatientInfo = async () => {
  const res = await PatientInfoAPI(props.id)
  addPatientParams.value.defaultFlag = res.data.defaultFlag
  addPatientParams.value.gender = res.data.gender
  addPatientParams.value.idCard = res.data.idCard
  addPatientParams.value.name = res.data.name
}
onLoad(() => {
  if (props.id) {
    PatientInfo()
  }
})
// 修改患者信息
const PatientUpdate = async () => {
  const res = await PatientUpdateAPI({ ...addPatientParams.value, id: props.id })
}
// 提交患者信息
const submit = async () => {
  await formRef.value?.validate()
  if (props.id) {
    await PatientUpdate()
    uni.showToast({ title: '编辑成功', icon: 'none' })
    uni.navigateTo({ url: '/subpkg_archive/pages/list/list' })
  } else {
    await PatientAdd()
    uni.showToast({ title: '添加成功', icon: 'none' })
    uni.navigateTo({ url: '/subpkg_archive/pages/list/list' })
  }
}
</script>

<template>
  <scroll-view>
    <view class="archive-page">
      {{ addPatientParams }}
      <uni-forms
        border
        label-width="220rpx"
        ref="formRef"
        :rules="rules"
        v-model="addPatientParams"
      >
        <uni-forms-item label="患者姓名" name="name">
          <uni-easyinput
            v-model="addPatientParams.name"
            placeholder-style="color: #C3C3C5; font-size: 32rpx"
            :input-border="false"
            :clearable="false"
            placeholder="请填写真实姓名"
          />
        </uni-forms-item>
        <uni-forms-item label="患者身份证号" name="idCard">
          <uni-easyinput
            v-model="addPatientParams.idCard"
            placeholder-style="color: #C3C3C5; font-size: 32rpx"
            :input-border="false"
            :clearable="false"
            placeholder="请填写身份证号"
          />
        </uni-forms-item>
        <uni-forms-item label="患者性别" name="gender">
          <uni-data-checkbox
            v-model="addPatientParams.gender"
            selectedColor="#16C2A3"
            :localdata="[
              { text: '男', value: 1 },
              { text: '女', value: 0 },
            ]"
          />
        </uni-forms-item>
        <uni-forms-item label="默认就诊人" name="defaultFlag">
          <view class="uni-switch">
            <switch
              :checked="addPatientParams.defaultFlag === 1"
              @change="changeSwitch"
              color="#20c6b2"
              style="transform: scale(0.7)"
            />
          </view>
        </uni-forms-item>
        <button class="uni-button" @click="submit">保存</button>
      </uni-forms>
    </view>
  </scroll-view>
</template>

<style lang="scss">
@import './index.scss';
</style>
